package com.example.teaching_server.controller;

import com.example.teaching_server.common.ApiResponse;
import com.example.teaching_server.dto.PageDTO;
import com.example.teaching_server.dto.ResourceDTO;
import com.example.teaching_server.dto.ResourceTypeDTO;
import com.example.teaching_server.service.ResourceService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/resources")
@RequiredArgsConstructor
@Tag(name = "资源管理", description = "资源文件查询和管理接口")
public class ResourceController {
    private final ResourceService resourceService;

    /**
     * 获取资源类型
     */
    @GetMapping("/types")
    @Operation(summary = "获取资源类型", description = "获取所有可用的资源类型列表")
    public ApiResponse<List<ResourceTypeDTO>> getResourceTypes() {
        log.info("开始获取资源类型");

        try {
            List<ResourceTypeDTO> types = resourceService.getAllResourceTypes();
            log.info("成功获取{}个资源类型", types.size());
            return ApiResponse.success("资源类型获取成功", types);
        } catch (Exception e) {
            log.error("获取资源类型失败", e);
            return ApiResponse.fail("资源类型获取失败: " + e.getMessage());
        }
    }

    /**
     * 获取文件列表
     */
    @GetMapping("/files")
    @Operation(summary = "获取文件列表", description = "根据类型分页获取资源文件列表")
    public ApiResponse<PageDTO<ResourceDTO>> getResources(
            @Parameter(description = "资源类型") @RequestParam(required = false) String type,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") int size) {

        log.info("开始获取文件列表: type={}, page={}, size={}", type, page, size);

        try {
            PageDTO<ResourceDTO> resources = resourceService.getResources(type, page, size);
            log.info("文件列表获取成功");
            return ApiResponse.success("文件列表获取成功", resources);
        } catch (Exception e) {
            log.error("获取文件列表失败: type={}", type, e);
            return ApiResponse.fail("文件列表获取失败: " + e.getMessage());
        }
    }

}
